﻿using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.DynamicData;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;
using Abhar.web.src;
using local = System.Globalization;
using System;
using System.Web.UI;

namespace Abhar.web
{
    public class Global : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            MetaModel model = new MetaModel();

            //                    IMPORTANT: DATA MODEL REGISTRATION 
            // Uncomment this line to register LINQ to SQL classes
            // model for ASP.NET Dynamic Data. Set ScaffoldAllTables = true only if you are sure 
            // that you want all tables in the data model to support a scaffold (i.e. templates) 
            // view. To control scaffolding for individual tables, create a partial class for 
            // the table and apply the [Scaffold(true)] attribute to the partial class.
            // Note: Make sure that you change "YourDataContextType" to the name of the data context
            // class in your application.
            model.RegisterContext(()=> new DatabaseContext(ConfigurationManager.ConnectionStrings[0].ConnectionString), new ContextConfiguration() {ScaffoldAllTables = true});
            // The following statement supports separate-page mode, where the List, Detail, Insert, and 
            // Update tasks are performed by using separate pages. To enable this mode, uncomment the following 
            // route definition, and comment out the route definitions in the combined-page mode section that follows.

            routes.Add(new DynamicDataRoute("Admin/{table}/{action}.aspx")
            {
                Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
                Model = model
            });

            // The following statements support combined-page mode, where the List, Detail, Insert, and
            // Update tasks are performed by using the same page. To enable this mode, uncomment the
            // following routes and comment out the route definition in the separate-page mode section above.
            //routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") {
            //    Action = PageAction.List,
            //    ViewName = "ListDetails",
            //    Model = model
            //});

            //routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") {
            //    Action = PageAction.Details,
            //    ViewName = "ListDetails",
            //    Model = model
            //});
        }

        protected void Application_Start(object sender, EventArgs e)
        {
            DatabaseContext context = new DatabaseContext(ConfigurationManager.ConnectionStrings[0].ConnectionString);
            if(!context.DatabaseExists())
                context.CreateDatabase();
            RegisterRoutes(RouteTable.Routes);
        }

       public void Global_PretRequestHandlerExecute(object sender, EventArgs e)
        {
           
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
        {

            if (HttpContext.Current.Request.RawUrl.ToLower().Contains("aspx") && !HttpContext.Current.Request.RawUrl.ToLower().Contains("admin") && !
                HttpContext.Current.Request.RawUrl.ToLower().Contains("dynamicdata")) 
            {
                Language lang;
                object tempLang = HttpContext.Current.Session["lang"];

                if (tempLang == null)
                    lang = Language.English;
                else
                    lang = (Language) tempLang;


                CultureInfo cultureInfo = lang == Language.Arabic ? new local.CultureInfo("ar-eg") : new local.CultureInfo("en-us");
                //----------------------------------------------------------
                string themeName;
                if (lang == Language.Arabic)
                    themeName = "Ar";
                else
                    themeName = "En";

                ((Page) HttpContext.Current.Handler).Theme = themeName;
     
                Thread.CurrentThread.CurrentCulture = cultureInfo;
                Thread.CurrentThread.CurrentUICulture = cultureInfo;
            }
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {
            //DatabaseContext context = new DatabaseContext(ConfigurationManager.ConnectionStrings[0].ConnectionString);
            //context.DeleteDatabase();
        }
    }
}